package cn.meibang.aggragation;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.CharEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

import cn.apestar.agg.common.interceptor.TokenConfig;
import cn.apestar.agg.common.interceptor.TokenInterceptor;
import cn.apestar.agg.common.security.LoginRequireInterceptor;
import cn.apestar.core.conf.CommonConfig;
import cn.apestar.core.framework.DefaultSiteWebConfig;
import cn.apestar.core.framework.request.RequestContext;
import cn.apestar.core.framework.request.RequestContextInterceptor;
import cn.apestar.core.http.AppType;
import cn.apestar.pay.provider.conf.AlipayConfig;
import cn.apestar.pay.provider.conf.WechatPayConfig;
import cn.apestar.sms.provider.conf.YunPianConfig;

/**
 * 配置文件
 * @author Kent
 */
@Configuration
public class WebConfig extends DefaultSiteWebConfig {

    @Autowired
    Environment environment;

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxInMemorySize(4096);
        // multipartResolver.setMaxUploadSize(1048576 * 2); // 最大尺寸为2M
        return multipartResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/libs/**").addResourceLocations("/libs/");
        registry.addResourceHandler("/module/**").addResourceLocations("/module/");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Bean
    public DefaultServletHttpRequestHandler defaultServletHttpRequestHandler() {
        DefaultServletHttpRequestHandler delDefaultServletHttpRequestHandler = new DefaultServletHttpRequestHandler();
        return delDefaultServletHttpRequestHandler;
    }

    @Override
    public void addCustomInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginRequireInterceptor());
        // registry.addInterceptor(tokenInterceptor());
        // registry.addInterceptor(loginRequireInterceptor());
    }

    @Override
    @Bean
    @Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public RequestContext requestContext() {
        return new RequestContext();
    }

    @Bean
    public CommonConfig commmonConfig() {
        CommonConfig commmonConfig = new CommonConfig();
        commmonConfig.setAppType(environment.getProperty("app.type"));
        commmonConfig.setPath(environment.getProperty("app.path"));
        commmonConfig.setThirdUrl(environment.getProperty("app.url"));
        Map mapUrl = new HashMap();
        if (AppType.meibang.getValue().equals(commmonConfig.getAppType())) {
            mapUrl.put(AppType.meibang.getValue(), environment.getProperty("app.url"));
            mapUrl.put(AppType.MEIBANG_LY.getValue(), environment.getProperty("app.url.ly"));
            mapUrl.put(AppType.MEIBANG_TEST.getValue(), environment.getProperty("app.url.test"));
        }
        commmonConfig.setMapUrl(mapUrl);
        return commmonConfig;
    }

    @Bean
    public LoginRequireInterceptor loginRequireInterceptor() {
        return new LoginRequireInterceptor();
    }

    @Override
    @Bean
    public RequestContextInterceptor requestContextInterceptor() {
        return new RequestContextInterceptor();
    }

    @Bean
    public AlipayConfig alipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        // nothing
        return alipayConfig;
    }

    @Bean
    public WechatPayConfig wechatPayConfig() {
        WechatPayConfig wechatPayConfig = new WechatPayConfig();
        return wechatPayConfig;
    }

    @Override
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        URL path = this.getClass().getResource("/");
        messageSource.setBasenames("classpath:messages/common_messages", "classpath:messages/messages", "classpath:messages/seller",
                                   "classpath:messages/task");
        messageSource.setDefaultEncoding(CharEncoding.UTF_8);
        return messageSource;
    }

    @Bean
    public YunPianConfig yunPianConfig() {
        YunPianConfig smsConfig = new YunPianConfig();
        smsConfig.setAccount(environment.getRequiredProperty("yp.account"));
        smsConfig.setPassword(environment.getRequiredProperty("yp.passwd"));
        smsConfig.setBaseUrl(environment.getRequiredProperty("yp.url"));
        smsConfig.setApiKey(environment.getRequiredProperty("yp.apikey"));
        return smsConfig;
    }

    @Bean
    public TokenConfig tokenConfig() {
        TokenConfig tokenConfig = new TokenConfig();
        tokenConfig.setKey("abcdefl234567hhh");
        tokenConfig.init();
        return tokenConfig;
    }

    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
    }

}
